// 文件：Assets/Editor/FolderIconOverride.cs
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

[InitializeOnLoad]
public static class FolderIconOverride
{
    [System.Serializable]
    private class IconMapping
    {
        public string targetGUID;
        public string iconGUID;
    }
    [System.Serializable]
    private class MappingList
    {
        public List<IconMapping> list = new List<IconMapping>();
    }

    static Dictionary<string, Texture2D> _iconMap;

    static FolderIconOverride()
    {
        LoadMapFromPrefs();
        EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
    }

    static void LoadMapFromPrefs()
    {
        _iconMap = new Dictionary<string, Texture2D>();
        var json = EditorPrefs.GetString("FolderIconManager_Mappings", "");
        if (string.IsNullOrEmpty(json)) return;
        var wrapper = JsonUtility.FromJson<MappingList>(json);
        if (wrapper?.list == null) return;

        foreach (var m in wrapper.list)
        {
            if (string.IsNullOrEmpty(m.targetGUID) || string.IsNullOrEmpty(m.iconGUID))
                continue;

            var iconPath = AssetDatabase.GUIDToAssetPath(m.iconGUID);
            var icon = AssetDatabase.LoadAssetAtPath<Texture2D>(iconPath);
            if (icon != null)
                _iconMap[m.targetGUID] = icon;
        }
    }

    static void ProjectWindowItemOnGUI(string guid, Rect selectionRect)
    {
        if (_iconMap != null && _iconMap.TryGetValue(guid, out var icon))
        {
            // 1. 计算 iconRect（和之前保持一致）
            float regionSize   = Mathf.Min(selectionRect.width, selectionRect.height);
            float iconSize     = regionSize * 0.35f;
            float margin       = regionSize * 0.08f;
            var iconRect = new Rect(
                selectionRect.x + regionSize - iconSize - margin,
                selectionRect.y + regionSize - iconSize - margin,
                iconSize, iconSize
            );

            // 2. 描边厚度（可根据实际缩放微调，比如 1 或 1.5 像素）
            float thickness = 0.5f;

            // 3. 先用黑色多次偏移绘制：上下左右 + 4 个对角
            var prevColor = GUI.color;
            GUI.color = Color.black;
            for (int dx = -1; dx <= 1; dx++)
            for (int dy = -1; dy <= 1; dy++)
            {
                if (dx == 0 && dy == 0) continue;
                var outlineRect = new Rect(
                    iconRect.x + dx * thickness,
                    iconRect.y + dy * thickness,
                    iconRect.width,
                    iconRect.height
                );
                GUI.DrawTexture(outlineRect, icon);
            }

            // 4. 再画一次你想要的颜色贴图
            GUI.color = new Color(1f, 1f, 1f, 1f); // 比如亮黄色
            GUI.DrawTexture(iconRect, icon);

            // 5. 恢复原色
            GUI.color = prevColor;
        }
    }
    public static void ReloadIcons()
    {
        LoadMapFromPrefs();
        EditorApplication.RepaintProjectWindow();
    }

}
#endif